/*
 * Title:        CloudScheduler Toolkit
 * Description:  a Toolkit for Modeling and Simulation of Job Scheduling and Resource Provisioning in Cloud System
 * Licence:      GPL - http://www.gnu.org/copyleft/gpl.html
 * Author:       Fuhui Wu
 *
 * Copyright (c) 2013-2014, The National University of Defense Technology, China
 */

package org.nudt.jCloud.resourceManager;

import java.util.ArrayList;
import java.util.List;

import org.nudt.jCloud.infrastructure.Cluster;
import org.nudt.jCloud.simulation.CloudSchedSIM;
import org.nudt.jCloud.simulation.SIMTag;
import org.nudt.jCloud.simulation.ScaleData;
import org.nudt.jCloud.simulation.SimEvent;

public abstract class ReservationRM extends ResManager{

	public ReservationRM(String name, Cluster cluster) {
		super(name, cluster);
	}
	
	@Override
	public void processEvent(SimEvent event) {
		switch(event.getTag()){
			case SIMTag.SCALE_RESOURCE:
				List<ScaleData> scaleDataList = new ArrayList<ScaleData>();
				scaleDataList.add((ScaleData)event.getData());
				List<SimEvent> scaleEventList = CloudSchedSIM.getDeferred().get(event.getDst(),
						SIMTag.SCALE_RESOURCE);
				for(SimEvent scaleEvent : scaleEventList){
					scaleDataList.add((ScaleData)scaleEvent.getData());
				}
				CloudSchedSIM.getDeferred().removeEventList(scaleEventList);
				scaleResource(scaleDataList);
				for(ScaleData scaleData : scaleDataList){
					sendNow(scaleData.getUserBrokerId(), SIMTag.SCALE_RESOURCE_ACK, scaleData);
				}
				break;
			default:
				throw new RuntimeException("Unsupported event in resource manager.");
		}
		
	}
	
	public abstract void scaleResource(List<ScaleData> scaleDataList);

}
